import { PageModalService } from './../../shared/service/page-modal-service';
import { Component, Input, OnInit } from '@angular/core';
import { Subscription } from 'rxjs';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { HttpService } from 'src/app/shared/service/http.service';
import { MessageService } from 'src/app/shared/service/message.service';

@Component({
  selector: 'app-application-admin-list',
  templateUrl: './application-admin-list.component.html',
  styleUrls: ['./application-admin-list.component.scss'],
})
// tslint:disable
export class ApplicationAdminListComponent implements OnInit {
  @Input() tagData: any;
  pageNo: any = 1;
  params: any;
  dataIndex : any;
  sessionId = localStorage.getItem('sessionId');
  subscription: Subscription = null;
  constructor(
    private appGlobalService: AppGlobalService,
    private pageModalService: PageModalService,
    private message: MessageService,
    private http: HttpService) {
  }

  ngOnInit() {
    this.subscribeMessage();
  }

  subscribeMessage() {
    this.params = this.tagData.data.submit;
    this.params.sessionId = localStorage.getItem("sessionId");
    if (this.appGlobalService.needAutoChoosenTab) {
      if (this.appGlobalService.globalSubscription && this.appGlobalService.globalSubscription.closed === false) {
        this.appGlobalService.globalSubscription.unsubscribe();
      }
    }
    if (!this.subscription && this.tagData.data.submit !== undefined) {
      this.message.clearMessage();
      this.subscription = this.message.getMessage().subscribe(param => {
        if (param === undefined) {
          return;
        } else if (param.type === 'ionRefresh') {
          return;
        }
        this.pageNo = 1;
        this.params.sessionId = localStorage.getItem("sessionId");
        this.params.content = param;
        this.params.content.pageNo = this.pageNo;
        this.http.post(
          "application/submit",
          this.params,
          res => {
            console.log("submit");
            if (res.success) {
              if (Array.isArray(res.data.data) && res.data.data.length > 0) {
                this.tagData.data.data = res.data.data;
                this.params = res.data.submit;
              } else {
                this.tagData.data.data = res.data.data;

              }

            } else {
              this.appGlobalService.showToast(res.message, 2000, "middle");
            }
          },
          err => {
            this.appGlobalService.showToast(err.message, 2000, "middle");
          }
        );
      });
      this.subscription['uuid'] = '' + new Date().getTime();
      console.log('ApplicationAdminList-' + this.subscription['uuid']);
      this.appGlobalService.tab5Subscription = this.subscription;
    }
  }
  goPage(item,i) {
    this.dataIndex = i;   
    this.pageModalService.openPage(item,'',res =>{
      this.updateItem();
    });
  }

  updateItem() {
    
    const that = this;

    if (!this.tagData.data.data[that.dataIndex].id){
      return;
    }
    if (that.tagData.data.data[that.dataIndex].updateItemType !== undefined
      && that.tagData.data.data[that.dataIndex].updateItemType === 'delete') {
      that.tagData.data.data.splice(this.dataIndex, 1);
      return;
    }
    const page: any = this.appGlobalService.getCurrentTarget();
    if (page.params) {
    } else {
      page.params = {}
    }
    page.params.id = this.tagData.data.data[that.dataIndex].id;
    this.http.post('application/page', page, async res => {
      const tagData = JSON.parse(res.script).views[0].tags;
      tagData.forEach(element => {
        if (element.style === 'ljManageUserListTag' && element.data && element.data.data[0]) {
          that.tagData.data.data.splice(that.dataIndex, 1, element.data.data[0]);
        }

      });
    });
  }

  loadMoreData(event) {
    this.params.sessionId = this.sessionId;
    this.pageNo = this.pageNo + 1;
    this.params.content.pageNo = this.pageNo;
    this.http.post('application/submit', this.params, res => {
      if (res.success) {
        if (Array.isArray(res.data.data) && res.data.data.length > 0) {
          res.data.data.map(item => {
            this.tagData.data.data.push(item);
          });
          this.params = res.data.submit;
        } else {
        }
        event.target.complete();
      } else {
        this.pageNo = this.pageNo - 1;
        event.target.complete();
      }
    });

  }
  ngOnDestroy(): void {
    if (this.subscription) {
      this.subscription.unsubscribe();
      this.appGlobalService.tab5Subscription = this.subscription;
      console.log('ApplicationAdminListD' + this.subscription['uuid']);
    }
  }
}
